<?php

namespace app\admin\controller;

use \think\Db;
use \think\Cookie;
use \think\Session;
use app\admin\model\Admin as adminModel;//管理员模型
use app\admin\model\AdminMenu;
use app\admin\controller\Permissions;

class Mp extends Permissions
{
    //轮播图管理
    public function mp_img()
    {
        $path = \db('mp_img')->order('up_time desc')->select();

        $this->assign('a', $path);
        return $this->fetch();
    }

    //添加轮播图
    public function mp_add()
    {
        if (request()->isPost()) {
            $id = $_POST['thumb'];
            $title = $_POST['title'];
            $arr = \db('attachment')
                ->field('filepath,create_time')
                ->where('id', $id)
                ->select();
            $create_time = array_column($arr, 'create_time');
            $create_time = implode(",", $create_time);
            $filepath = array_column($arr, 'filepath');
            $filepath = implode(",", $filepath);
            $time = date("Y-m-d H:i:s", $create_time);
            $data = [
                'title' => $title,
                'path' => $filepath,
                'up_time' => $time
            ];
            $img = \db('mp_img')->insert($data);
            if (empty($img)) {
                return $this->error('添加失败', 'admin/mp/mp_add');
            } else {
                return $this->success('添加成功', 'admin/mp/mp_img');
            }
        }
        return $this->fetch();
    }

    //轮播图修改
    public function mp_update()
    {
        if (request()->isPost()) {
            $img_id = $_POST['id'];
            if (empty($_POST['thumb'])) {
                $title = $_POST['title'];
                $create_time = time();
                $time = date("Y-m-d H:i:s", $create_time);
                $img = \db('mp_img')
                    ->where('img_id', $img_id)
                    ->update([
                        'up_time' => $time,
                        'title' => $title,
                    ]);
                if ($img >= 0) {
                    return $this->success('修改成功', 'admin/mp/mp_img');
                }
            } else {
                $id = $_POST['thumb'];
                $title = $_POST['title'];
                $arr = \db('attachment')
                    ->field('filepath,create_time')
                    ->where('id', $id)
                    ->select();
                $create_time = array_column($arr, 'create_time');
                $create_time = implode(",", $create_time);
                $filepath = array_column($arr, 'filepath');
                $filepath = implode(",", $filepath);
                $time = date("Y-m-d H:i:s", $create_time);
                $img = \db('mp_img')
                    ->where('img_id', $img_id)
                    ->update([
                        'path' => $filepath,
                        'up_time' => $time,
                        'title' => $title,
                    ]);
                if ($img >= 0) {
                    return $this->success('修改成功', 'admin/mp/mp_img');
                }
            }

        } else {
            $id = input('id');
            $info = \db('mp_img')->where('img_id', $id)->find();

            $this->assign('info', $info);
            return $this->fetch();
        }
    }

    //轮播图删除
    public function mp_delete()
    {
        $id = input('id');
        $admin = \db('mp_img')->where('img_id', $id)->delete();
        if ($admin >= 1) {
            $this->success('删除成功', 'mp/mp_img');
        } else {
            $this->error('删除失败');
        }

    }

}
